<?php

namespace backend\controllers;

use backend\components\Certificate;
use backend\components\UploadMedia;
use yii\web\Controller;

class SmallController extends Controller
{
    public function actionCert()
    {
        /**
         * string(68) "/data/wwwroot/default/teach/teach/components/cert/apiclient_cert.pem"
        string(67) "/data/wwwroot/default/teach/teach/components/cert/apiclient_key.pem"
        array(7) {
        ["return_code"]=>
        string(7) "SUCCESS"
        ["return_msg"]=>
        string(2) "OK"
        ["result_code"]=>
        string(7) "SUCCESS"
        ["nonce_str"]=>
        string(16) "m9vVnOVWt3LHu9Jt"
        ["sign"]=>
        string(64) "CBAEE6C98DBD2E1446CA133D99756235FBCDF18D25216A9488E5AD65BB6FC119"
        ["mch_id"]=>
        string(10) "1528625341"
        ["certificates"]=>
        string(2156) "{"data":[{"serial_no":"756FFD92D22299D8B9E6B9D7944C9720F7F4DAED","effective_time":"2019-05-30 10:05:40","expire_time":"2024-05-28 10:05:40","encrypt_certificate":{"algorithm":"AEAD_AES_256_GCM","nonce":"f1afc26f9592","associated_data":"certificate","ciphertext":"SkPM6I9AlFutvv9Uu+KJNwVJnFUGPyyiGW7ugYF3DYhOa4bdlZyEDnTpcFuHK7/2kkVIOcweiJxb4R+LFN5/rWAfgRRnvaxOGM6v2sbW/sQkNbrzg05QQ1wrnx4ok0NXNb+IXKe1LUzX52lkkL8NIu/ljwuajlyZcUtz8wdt1yHJ2nUF/45AW3I8AIXk3FSXzedJaYz0WFsfGYfBhf9uvxi7S1hILQ4YwlVht3W8fP9d7+rwINUu6aAzYP+lyWN3St4wCbMhDJhyzanhjHo9CAfwuF4rxayaF1+TiXn8V0AqDx31zi4yienbmntDNlvDDYbMf6v9g0u7SBWZIE7eL5FahpK9ouaIHcHT7NjCXo8en7sfJEKlLwMd16tdMWaRGl59v/ZRrYFpA+OfKkYAaHrakWrVK224CyHTWLtcASikOgmjsNzbdHrK/eOwKESYlxVw8085qxcT7R1zngZtKVGS8ZTpTFFCDdK+oHwwnvKtJlGXacn2O0FZw6gEqM9a0XbmL87Sp5w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cUFlFOwstkj410vRashYV+U8KtHjVbydmYNqPB2bHOZ9aTjA29RIAoqYwfwimxufS06xJIHaiRJa0vCQG7pWgvp8xkBFNXHp+w6pYVPc6ExckSDEx0gSvc+25676mklONf7PmK64QAzFNphw1y35cB1w5elojMf0CSKF3ZO2zBTsN761de+5ZKRWJk/0dbgZqz7rOVb8veZfBvA8uqfqWfLM3S76aUqhX6+Km16WBFVdaeJx/yrfAue1PtyCFmbrTlOuKvNGaE9ks7ytFXkOl5l2GpA=="}}]}"
        }
         */
        $Certificat = new Certificate();
        $result = $Certificat->downloadCertificates();
        var_dump($result);
    }

    public function actionUpload()
    {
        /**
         * <xml><return_code><!--[CDATA[SUCCESS]]--></return_code>
        <return_msg><!--[CDATA[OK]]--></return_msg>
        <result_code><!--[CDATA[SUCCESS]]--></result_code>
        <media_id><!--[CDATA[MHhGUrVO_dloXtaOAZ51w0RaCJ5Fx0DCCBD8ZaJ1r3u0vw2xFrwF1H84j8ESW6dyGN4-GMFS4L_N-rB3NCdaMumCqSJ0BwDZ8a1BYRA5t_s]]--></media_id>
        <sign><!--[CDATA[717A32F50B357103775096FF8EB0143D]]--></sign>
        </xml>
         *
         * array(5) {
        ["return_code"]=>
        string(7) "SUCCESS"
        ["return_msg"]=>
        string(2) "OK"
        ["result_code"]=>
        string(7) "SUCCESS"
        ["media_id"]=>
        string(107) "MHhGUrVO_dloXtaOAZ51w3T3K7kgzHPHViZrsy5fsr8oY_TDJdJJeavRqHoDLH4hBqyuVnzcWgNwMzut2CoBb6RiCB_6Omtw41WiZSpQckE"
        ["sign"]=>
        string(32) "0C700F019B28EEC09E6F62FE11F1674B"
        }
         */
        $UploadMedia = new UploadMedia();
        $result = $UploadMedia->uploadImg();
        var_dump($result);
    }

    /**
     * publicKeyEncrypt 对身份证等敏感信息加密 //在下载的服务商证书中加入到此
     * @param string $string
     * @return string | false
     */
    protected function publicKeyEncrypt(string $string)
    {
        $crypted   = '';
        $publicKey = $this->getPublicKey();
        if ($publicKey) {
            $publicKeyResource = openssl_get_publickey($publicKey);
            $f                 = openssl_public_encrypt($string, $crypted, $publicKeyResource, OPENSSL_PKCS1_PADDING);
            openssl_free_key($publicKeyResource);
            if ($f) {
                return base64_encode($crypted);
            }
        }
        return false;
    }

    /**
     * getPublicKey 获取上一次本地保存的公钥
     * @return bool|string
     */
    protected function getPublicKey()
    {
        if (file_exists($this->publicKeyAddr)) //将下载的cert 证书内容弄个放到此处
            return $this->publicKey ? : $this->publicKey = file_get_contents($this->publicKeyAddr);
        return '';
    }
}